<template>
  <el-dialog title="分配角色" :visible.sync="assignRoleVisible" @close="btnCancel">
    <!-- 多选框 -->
    <el-checkbox-group v-model="rolesId">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <!-- footer -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button @click="btnCancel">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees.js'
export default {
  props: {
    assignRoleVisible: { // 控制弹层状态
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [], // 存放所有角色列表
      rolesId: [] // 存放选中的id
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    //  获取所有角色
    async getRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
    },
    // 获取当前角色的id
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.rolesId = roleIds // 赋值本用户的角色
    },
    btnCancel() {
      this.roleIds = [] // 清空原来的数组
      this.$emit('update:assignRoleVisible', false)
    },
    async btnOK() {
      await assignRoles({ id: this.userId, roleIds: this.rolesId })
      //   关闭窗体
      this.$emit('update:assignRoleVisible', false)
    }
  }

}
</script>

<style>

</style>
